package com.blac

import android.util.Base64
import com.blac.utils.ADManager
import com.blac.utils.ToastUtil
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec

object SDK {

    private const val ALGORITHM = "AES"
    private const val type = "AES/ECB/PKCS5Padding"

    // 16字节固定密钥（示例值，实际应替换）
    private val sec: ByteArray = "E2er2Dtw23CtCsd2".toByteArray(Charsets.UTF_8)

    //每次启动调用，首次同意协议调用
    @JvmStatic
    fun initAd(){
        SDKManager.getInstance().initAd(SDKManager.mApplication)
    }

    @JvmStatic
    fun playAd(adSource : String){
        ADManager.playAd(adSource)
    }

    @JvmStatic
    fun showToast(message:String){
        ToastUtil.showToast(message)
    }

    fun encrypt(plaintext:String):String{
        return try {
            val key = SecretKeySpec(sec, ALGORITHM)
            val cipher = Cipher.getInstance(type)
            cipher.init(Cipher.ENCRYPT_MODE,key)
            val encrypted=cipher.doFinal(plaintext.toByteArray(Charsets.UTF_8))
             Base64.encodeToString(encrypted,Base64.NO_WRAP)
        }catch (e:Exception){
            e.printStackTrace()
            return ""
        }
    }

    fun decrypt(ciphertext:String):String{
        return try {
            val cipher =Cipher.getInstance(type)
            cipher.init(Cipher.DECRYPT_MODE,SecretKeySpec(sec, ALGORITHM))
            val decoded =Base64.decode(ciphertext,Base64.NO_WRAP)
             String(cipher.doFinal(decoded), Charsets.UTF_8)
        }catch (e:Exception){
            e.printStackTrace()
            return ""
        }
    }
}